//
//  GKZYMyInfoDataViewController.m
//  GKZY_CLASS
//
//  Created by chenziqiang on 15/12/5.
//  Copyright © 2015年 eenet. All rights reserved.
//
#import "GKZY_CLASS.h"
#import "GKZYMyInfoDetailViewController.h"
#import "GKZYLoginUserInfoEntity.h"
#import "GKZYMyInfoHeaderCell.h"
#import "GKZYMyInfoModel.h"
#import "GKZYMyInfoCell.h"
#import "ResourceHelper.h"
#import "ClassHelper.h"
#import <Masonry/Masonry.h>


#pragma mark - OOMyInfoDetailViewController

@interface GKZYMyInfoDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *dataTableView;
@property (nonatomic, strong) NSMutableArray *datas;
@end

@implementation GKZYMyInfoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人资料";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.dataTableView];
    [self.dataTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    [self initialData];
}

/**
 * 初始化数据
 */
- (void)initialData{
    NSMutableArray *dataArray = [NSMutableArray array];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSData *currentStudentInfoData = [userDefaults valueForKey:OO_USER_INFO_KEY];
    GKZYLoginUserInfoEntity *userInfoEntity = [NSKeyedUnarchiver unarchiveObjectWithData:currentStudentInfoData];
    
    NSMutableArray *row1 = [NSMutableArray array];
    GKZYMyInfoModel *model11 = [GKZYMyInfoModel new];
    if (userInfoEntity.header && ![userInfoEntity.header isKindOfClass:[NSNull class]]) {
        model11.img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userInfoEntity.header]]];
    }
    model11.imgName = @"headNew";
    model11.imgSuffix = @"png";
    model11.title = @"头像";
    model11.showArrows = NO;
    model11.isHeader = YES;
    model11.isDetail = YES;
    [row1 addObject:model11];
    
    GKZYMyInfoModel *model12 = [GKZYMyInfoModel new];
    model12.title = @"个性签名";
    model12.subTitle = @"";
    model12.showArrows = NO;
    model12.isHeader = NO;
    model12.isDetail = YES;
    [row1 addObject:model12];
    [dataArray addObject:row1];
    
    NSMutableArray *row2 = [NSMutableArray array];
    GKZYMyInfoModel *model21 = [GKZYMyInfoModel new];
    model21.title = @"姓名";
    model21.subTitle = userInfoEntity.name;
    model21.showArrows = NO;
    model21.isHeader = NO;
    model21.isDetail = YES;
    [row2 addObject:model21];
    
    GKZYMyInfoModel *model22 = [GKZYMyInfoModel new];
    model22.title = @"性别";
    model22.subTitle = userInfoEntity.sex;
    model22.showArrows = NO;
    model22.isHeader = NO;
    model22.isDetail = YES;
    [row2 addObject:model22];
    
    GKZYMyInfoModel *model23 = [GKZYMyInfoModel new];
    model23.title = @"年龄";
    model23.subTitle = @"30";
    model23.showArrows = NO;
    model23.isHeader = NO;
    model23.isDetail = YES;
    [row2 addObject:model23];
    
    GKZYMyInfoModel *model24 = [GKZYMyInfoModel new];
    model24.title = @"出生年月";
//    model24.subTitle = userInfoEntity.birthday;
    model24.showArrows = NO;
    model24.isHeader = NO;
    model24.isDetail = YES;
    [row2 addObject:model24];
    
    GKZYMyInfoModel *model25 = [GKZYMyInfoModel new];
    model25.title = @"所在班级";
//    model25.subTitle = userInfoEntity.teachingClassName;
    model25.showArrows = NO;
    model25.isHeader = NO;
    model25.isDetail = YES;
    [row2 addObject:model25];
    
    GKZYMyInfoModel *model26 = [GKZYMyInfoModel new];
    model26.title = @"联系手机";
    model26.subTitle = userInfoEntity.phone;
    model26.showArrows = NO;
    model26.isHeader = NO;
    model26.isDetail = YES;
    [row2 addObject:model26];
    
    GKZYMyInfoModel *model27 = [GKZYMyInfoModel new];
    model27.title = @"联系邮箱";
    model27.subTitle = ![userInfoEntity.email isKindOfClass:[NSNull class]] ? userInfoEntity.email : @"";
    model27.showArrows = NO;
    model27.isHeader = NO;
    model27.isDetail = YES;
    [row2 addObject:model27];
    [dataArray addObject:row2];
    
//    NSMutableArray *row3 = [NSMutableArray array];
//    OOMyInfoModel *model31 = [OOMyInfoModel new];
//    model31.title = @"修改密码";
//    model31.showArrows = YES;
//    model31.isHeader = NO;
//    model31.isDetail = YES;
//    [row3 addObject:model31];
//    [dataArray addObject:row3];
    
    self.datas = dataArray;
}

#pragma mark - UITableView DataSourse
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *row = self.datas[section];
    return row.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GKZYMyInfoModel *model = [self.datas[indexPath.section] objectAtIndex:indexPath.row];
    if (indexPath.section == 0 && indexPath.row == 0) {
        NSString *cellName = @"header";
        GKZYMyInfoHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
        if (!cell) {
            cell = [[GKZYMyInfoHeaderCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
        }
        cell.hiddenTopLine = indexPath.row != 0 && !model.isHeader;
        cell.data = model;
        return cell;
    }else{
        NSString *cellName = @"myInfo";
        GKZYMyInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
        if (!cell) {
            cell = [[GKZYMyInfoCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellName];
        }
        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
        cell.hiddenTopLine = indexPath.row != 0 && !model.isHeader;
        cell.data = model;
        
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 14;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 70;
    }else{
        return 49;
    }
}

#pragma mark - Property Setter
- (void)setDatas:(NSMutableArray *)datas{
    _datas = datas;
    if (datas) {
        [self.dataTableView reloadData];
    }
}
#pragma mark - Property Getter
- (UITableView *)dataTableView{
    if (!_dataTableView) {
        _dataTableView = ({
            UITableView *table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
            table.delegate = self;
            table.dataSource = self;
            table.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
            table.rowHeight = UITableViewAutomaticDimension;
            table.estimatedRowHeight = 50;
            table;
        });
    }
    return _dataTableView;
}

@end
